#include "Plane.h"

Plane::Plane()
{
  xSize = 0;
  ySize = 0;
  xRes = 2;
  yRes = 2;
  
  normal = CoordF( 0.0, 0.0, 1.0 );
  rotation = CoordF( 0.0, 0.0, 0.0 );
  setPosCenter( CoordF(0.0, 0.0, 0.0) );
}

void Plane::setNormal ( CoordF n )
{
     normal = n;
     normal.normalize();
}
             
void Plane::setPosUpperLeft ( CoordF p )
{
     posUpperLeft = p;
     posCenter = CoordF( p.x + (xSize/2), p.y - (ySize/2), p.z);
}

void Plane::setPosCenter ( CoordF p )
{
     posCenter = p;
     posUpperLeft = CoordF( p.x - (xSize/2), p.y + (ySize/2), p.z);
}
             
void Plane::resize ( GLfloat xFactor, GLfloat yFactor )
{
     xSize *= xFactor;
     ySize *= yFactor;
     
     setPosCenter(posCenter);
}

void Plane::rotate ( CoordF r )
{
     rotation += r;
}

void Plane::draw()
{
     mMat.initMaterial();

     GLfloat xStep = xSize / xRes;
     GLfloat yStep = ySize / yRes;
     
     CoordF temp = CoordF( -xSize/2, ySize/2 );

     glPushMatrix();
       glTranslatef( posCenter.x, posCenter.y, posCenter.z );
       glRotatef( rotation.x, 1, 0, 0 );
       glRotatef( rotation.y, 0, 1, 0 );
       glRotatef( rotation.z, 0, 0, 1 );     
       glTranslatef( temp.x, temp.y, temp.z );
       
       for (GLfloat y = 0.0; y < ySize; y += yStep)
       {
         glBegin(GL_TRIANGLE_STRIP);
         for (GLfloat x = 0.0; x <= xSize; x += xStep)
         {
             mMat.setMatNormal(normal);
             mMat.setMatCoord2D(x, xSize, y, ySize);
             glVertex3d(x, -y, 0.0);
             
             mMat.setMatCoord2D(x, xSize, y+yStep, ySize);
             glVertex3d(x, -y - yStep, 0.0);
         }
         glEnd();
       }
    
     glPopMatrix();

     mMat.finishMaterial();
}
